package com.inspur.edp.common.expr.expresses.logic;


import com.inspur.edp.common.expr.Express;
import com.inspur.edp.common.expr.ExpressDSLGenerator;
import com.inspur.edp.common.expr.ExpressDSLGeneratorMgr;
import com.inspur.edp.common.expr.ast.ASTContext;

/**
 * @author lizhaorui
 * @description TODO
 * @date 2024/12/17 10:26
 */
public class LogicExprDSL implements ExpressDSLGenerator {

    @Override
    public String getKind() {
        return LogicExpr.KIND;
    }

    @Override
    public String generate(ASTContext context, Express express) {
        LogicExpr logicExpr = (LogicExpr) express;
        String logicOp = logicExpr.getOperator() == LogicOperator.AND ? "&&" : "||";
        StringBuilder sb = new StringBuilder();
        if (logicExpr.getExpresses().size() > 0) {
            int index = 0;
            for (Express expressItem : logicExpr.getExpresses()) {
                if (index > 0) {
                    sb.append(logicOp);
                }
                boolean isLogicExpr = expressItem instanceof LogicExpr;
                String expressItemDSL = ExpressDSLGeneratorMgr.generateDsl(context, expressItem);
                if (isLogicExpr) {
                    sb.append(" ( " + expressItemDSL + " ) ");
                } else {
                    sb.append(expressItemDSL);
                }
                index++;
            }
        } else {
            sb.append("true");
        }
        return sb.toString();
    }
}
